﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using VocabManager.ViewModel;
using Microsoft.Phone.Shell;
using VocabManager.Code;
using System.Windows.Data;
using System.Globalization;
using VocabManager.Voice;

namespace VocabManager.View
{
    public partial class ItemViewModeView : UserControlBase
    {
        ItemViewModel _itemViewModel;
        internal ApplicationBarIconButton _folderButton;
        ApplicationBarMenuItem _deleteMenuItem;
        public ItemViewModeView()
        {
            InitializeComponent();
            _folderButton = new ApplicationBarIconButton(new Uri(IconUrls.Folder, UriKind.Relative));
            _folderButton.Text = "deck";
            _folderButton.Click += new EventHandler(_folderButton_Click);
            
        }

        void _folderButton_Click(object sender, EventArgs e)
        {
            _itemViewModel.DeckViewModel.CardItems.MoveCurrentToPosition(-1);
        }

        void ItemViewModeView_Unloaded(object sender, RoutedEventArgs e)
        {
            _itemViewModel.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(_currentItem_PropertyChanged);
        }

        void _currentItem_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            
            if (e.PropertyName == "IsFrontSide")
            {

                UpdateUI();
            }

        }

        private void UpdateUI()
        {
            if (_itemViewModel.IsFrontSide)
            {
                bdBack.Visibility = System.Windows.Visibility.Collapsed;
                bdFront.Visibility = System.Windows.Visibility.Visible;

            }
            else
            {
                bdBack.Visibility = System.Windows.Visibility.Visible;
                bdFront.Visibility = System.Windows.Visibility.Collapsed;
            }
        }
       
        protected override void OnLoad()
        {
            base.OnLoad();
            _itemViewModel =this.DataContext as ItemViewModel;
            _itemViewModel.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(_currentItem_PropertyChanged);
            UpdateUI();
            StoreAppBarButtons();
            ContainerPage.ApplicationBar.Buttons.Clear();
          //ItemView parent  UIHelper.FindVisualParent<ItemView>(this)

            ContainerPage.ApplicationBar.Buttons.Add(DeckViewContainer._previousButton);
            
            ContainerPage.ApplicationBar.Buttons.Add(_folderButton);
            ContainerPage.ApplicationBar.Buttons.Add(DeckViewContainer._editButton);
           
            ContainerPage.ApplicationBar.Buttons.Add(DeckViewContainer._nextButton);

            _deleteMenuItem = new ApplicationBarMenuItem();
            _deleteMenuItem.Text= "-Delete Card Item";
            _deleteMenuItem.Click+=new EventHandler(_deleteMenuItem_Click);
            ContainerPage.ApplicationBar.MenuItems.Add(_deleteMenuItem);



        }

        void _deleteMenuItem_Click(object sender, EventArgs e)
        {
            _itemViewModel.DeckViewModel.RemoveCurrentCommand.Execute(null);
        }
        protected override void OnUnload()
        {
            base.OnUnload();
            
            _itemViewModel.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(_currentItem_PropertyChanged);
            ContainerPage.ApplicationBar.MenuItems.Remove(_deleteMenuItem);
        }

        DeckView _deckViewContainer;
        public DeckView DeckViewContainer
        {
            get
            {
                if (_deckViewContainer == null)
                    _deckViewContainer = UIHelper.FindVisualParent<DeckView>(this);
                return _deckViewContainer;
            }
        }

        private void LayoutRoot_Hold(object sender, GestureEventArgs e)
        {

        }

        private void btnPlay_Click(object sender, RoutedEventArgs e)
        {
            (new SoundManager(_itemViewModel.BackSound)).PlayRecording();
        }

        private void OnFlick(object sender, Microsoft.Phone.Controls.FlickGestureEventArgs e)
        {
                // User flicked towards left
                if (e.HorizontalVelocity < 0)
                {
                    _itemViewModel.DeckViewModel.NextCommand.Execute(null);
                   
                }

                // User flicked towards right
                if (e.HorizontalVelocity > 0)
                {
                    _itemViewModel.DeckViewModel.PereviousCommand.Execute(null);

                }
        }
        
    }
    public class StringToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType,
            object parameter, CultureInfo culture)
        {
            if (string.IsNullOrEmpty((string)(value)))
                return Visibility.Collapsed;
            else
                return Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType,
            object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
            // Do the conversion from visibility to bool
        }
    }
}
